#include"../include/p.h"
int db_zsgc(sqlite3 *mydb,int newfd)
{
		char zsgcbuf[50]={'\0'};
		while(1)
		{
			//接收传来的增删改查的消息
			printf("接收客户端的消息\n");
			memset(zsgcbuf,'\0',sizeof(zsgcbuf));
			recv(newfd,zsgcbuf,sizeof(zsgcbuf),0);

			printf("%s\n",zsgcbuf);
			if(strcmp(zsgcbuf,"create")==0)
			{
				printf("用户想创建新的表\n");
				//在数据库中新建表
				char newtable[50]={"请输入您要创建的表名"};
				printf("%s",newtable);
				send(newfd,newtable,sizeof(newtable),0);
				memset(newtable,'\0',sizeof(newtable));
			//	sleep(5);
				recv(newfd,newtable,sizeof(newtable),0);
				printf("%s",newtable);
				int res=create_mytb(mydb,newtable);
				if(res<0)
				{
					printf("create tb error ");
					return -1;
				}
				printf("newtable create ok\n");
				send(newfd,"your table create ok",strlen("your table create ok"),0);
			}
			else if(strcmp(zsgcbuf,"insert")==0)
			{
				printf("用户要插入表中数据\n");
				char tablebuf[50]={"请输入要插入表的的表名"};
				printf("%s\n",tablebuf);
				send(newfd,tablebuf,sizeof(tablebuf),0);
				bzero(tablebuf,sizeof(tablebuf));
				printf("\n");
				recv(newfd,tablebuf,sizeof(tablebuf),0);
				printf("%s\n",tablebuf);
				char bufin[50]={"请输入要插入的modid，名称和属性:"};
				//给用户反馈 
				send(newfd,bufin,sizeof(bufin),0);
				memset(bufin,'\0',sizeof(bufin));
				//接收传来的 字符串，在函数中进行解析并插入表中
				recv(newfd,bufin,sizeof(bufin),0);
				printf("传来的字符串为：：%s\n",bufin);
				int ret=myinsert(newfd,mydb,tablebuf,bufin);
				if(ret<0)
				{
					printf("插入失败！\n");

				}

 						
			}
			else if(strcmp(zsgcbuf,"delete")==0)
			{
				printf("用户要进行删除功能\n");
				char debuf[50]={"请输入要删除的表名"};
				send(newfd,debuf,sizeof(debuf),0);
				bzero(debuf,sizeof(debuf));
				//寻找表是否存在；
				//接受表名
				recv(newfd,debuf,sizeof(debuf),0);
				printf("%s\n",debuf);
			//	printf("%s\n",debuf);
			//	char debufid[50]={"请输入要删除的id"};
			//	send(newfd,debufid,sizeof(debufid),0);
			//	bzero(debufid,sizeof(debufid));
			//	recv(newfd,debufid,sizeof(debufid),0);
				//删除该表中id所在的行
				//删除表
				int ret=delete_db(mydb,debuf);
				if(ret<0)
				{
					return -1;
					send(newfd,"error",5,0);
				}
				else if(ret==2)
				{
					
					printf("数据库中没有此表\n");
					send(newfd,"no",2,0);
					
				}
				else
				{
					printf("drop ok");
					send(newfd,"ok",2,0);
				}
				


			}
			else if(strcmp(zsgcbuf,"select")==0)
			{
				printf("用户要查看表的信息\n");
				//让用户输入表名传过来
				char bufs[50]={"请输入要查看的表名"};
				send(newfd,bufs,sizeof(bufs),0);
				bzero(bufs,sizeof(bufs));
				recv(newfd,bufs,sizeof(bufs),0);
				int ret=select_db(mydb,bufs);
				if(ret<0)
				{
					printf("select error \n");
					send(newfd,"error",5,0);
					return-1;
				}
				else
				{
					printf("select ok\n");

					send(newfd,"ok",2,0);
				}

			}
			else if(strcmp(zsgcbuf,"quit")==0)
			{
				printf("用户退出增删改查功能\n");
				return 0;
			}
			else
			{
				printf("用户有问题\n");
				//return-1;
			}
		}
	return 0;
}

